package com.cqc.okokhttpdemo02.http;

import android.util.Log;

import com.cqc.okokhttpdemo02.event.HttpErrorEvent;
import com.cqc.okokhttpdemo02.event.HttpSuccessEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by ${cqc} on 2016/11/14.
 * OkHttp的get+post的封装
 */

public class MainRequest {

    private static MainRequest mainRequest;

    private MainRequest() {
        super();
    }

    public static MainRequest getInstance() {
        if (mainRequest == null) {
            mainRequest = new MainRequest();
        }
        return mainRequest;
    }

    //异步get
    public void makeAsyncGetRequest(Call call, final RequestTag tag) {
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                httpErrorEvent(e, tag);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                httpSuccessEvent(response.body().string(), tag);
            }
        });
    }

    //同步get
    public void makeSyncGetRequest(final Call call, final RequestTag tag) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = call.execute();
                    if (response.isSuccessful()) {
                        httpSuccessEvent(response.body().string(), tag);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    httpErrorEvent(e, tag);
                }
            }
        }).start();
    }

    //异步post
    public void makeSyncPostRequest(Call call, final RequestTag tag) {
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                httpErrorEvent(e, tag);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                httpSuccessEvent(response.body().string(), tag);
            }
        });
    }

    //同步post
    public void makeAsyncPostRequest(final Call call, final RequestTag tag) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = call.execute();
                    if (response.isSuccessful()) {
                        httpSuccessEvent(response.body().string(), tag);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    httpErrorEvent(e, tag);
                }
            }
        }).start();
    }


    private void httpErrorEvent(IOException e, RequestTag tag) {
        Log.d("error", "error=" + e.getMessage().toString());
        HttpErrorEvent event = new HttpErrorEvent();
        event.setErrorMessage("" + e.getMessage().toString());
        event.setRequestTag(tag);
        EventBus.getDefault().post(event);
    }

    private void httpSuccessEvent(String json, RequestTag tag) {
        Log.d("response", "response=" + json);
        HttpSuccessEvent event = new HttpSuccessEvent();
        event.setJson(json);
        event.setRequestTag(tag);
        EventBus.getDefault().post(event);
    }
}
